﻿Imports System.IO.Ports
Module CommModele 'COM端口模块
    Public Function GetAvaliableSerialPorts() As String() '获取端口名字 返回字符串数组
        Dim portNames() As String = SerialPort.GetPortNames()
        Array.Sort(portNames)
        Return portNames
    End Function
End Module
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '窗口创建完毕后 就进行初始化
        Control.CheckForIllegalCrossThreadCalls = False


        'Dim names() As String '定义字符串数组变量names 
        'names = GetAvaliableSerialPorts() '获取端口
        'cboPort.Items.AddRange(names) '向ComboBox加入
        cboPort.Items.Add("COM3")
        cboSpeed.SelectedIndex = 0
    End Sub



    Private Sub Reload_Click(sender As Object, e As EventArgs) Handles Reload.Click
        cboPort.Items.Clear() '刷新串口前 先清除所有
        Dim names() As String '定义字符串数组变量names 
        names = GetAvaliableSerialPorts() '获取端口
        cboPort.Items.AddRange(names) '向ComboBox加入
    End Sub

    Private Sub turnon_Click(sender As Object, e As EventArgs) Handles turnon.Click
        'Dim port As Integer
        'port = Val(cboPort.Text.Last())
        'MSComm1.CommPort = port

        'MSComm1.Settings = cboSpeed.Text
        'MSComm1.PortOpen = True
        'MSComm1.RThreshold = 2
        'MSComm1.SThreshold = 2



        SerialPort1.PortName = "COM3" '设置串口名
        SerialPort1.BaudRate = Val(cboSpeed.Text) '设置波特率
        SerialPort1.Open()

        turnon.Enabled = False
        turndown.Enabled = True
    End Sub

    Private Sub turndown_Click(sender As Object, e As EventArgs) Handles turndown.Click
        SerialPort1.Close()
        'MSComm1.PortOpen = False

        turndown.Enabled = False
        turnon.Enabled = True
    End Sub




    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        TextBox1.Text += SerialPort1.ReadChar()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SerialPort1.WriteLine("S0 M03")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SerialPort1.WriteLine("S400 M03")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SerialPort1.WriteLine(TextBox2.Text)
        'MSComm1.Output = TextBox2.Text + vbCrLf
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        SerialPort1.WriteLine("G92 X0 Y0 Z0")
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        SerialPort1.WriteLine("G91 X-" + xy.Text)
    End Sub

    Private Sub xx_Click(sender As Object, e As EventArgs) Handles xx.Click
        SerialPort1.WriteLine("G91 X" + xy.Text)
    End Sub



    Private Sub y_Click(sender As Object, e As EventArgs) Handles y.Click
        SerialPort1.WriteLine("G91 Y-" + xy.Text)
    End Sub

    Private Sub yy_Click(sender As Object, e As EventArgs) Handles yy.Click
        SerialPort1.WriteLine("G91 Y" + xy.Text)
    End Sub

    Private Sub back_Click(sender As Object, e As EventArgs) Handles back.Click
        SerialPort1.WriteLine("G90 X0 Y0")
    End Sub

    Private Sub MSComm1_OnComm(sender As Object, e As EventArgs) Handles MSComm1.OnComm
        Select Case MSComm1.CommEvent
            Case 2
                TextBox1.Text += MSComm1.Input
        End Select
    End Sub
End Class
